
package com.example.mvp.mvppromote.network;

import com.example.mvp.mvppromote.config.Constant;
import com.example.mvp.mvppromote.network.api.BrandApi;
import com.example.mvp.mvppromote.network.api.WebViewApi;
import com.example.mvp.mvppromote.network.api.WeiXinApi;
import com.example.mvp.mvppromote.utils.AppManager;
import com.example.mvp.mvppromote.utils.LogUtils;

import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wty on 2016/6/23 0023.
 */
public class Network {
    private static BrandApi brandApi;
    private static WebViewApi webViewApi;
    private static WeiXinApi weiXinApi;
    private static OkHttpClient okHttpClient = new OkHttpClient();
    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();

    public static BrandApi getBrandApi() {
        if (brandApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Constant.BAND_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            brandApi = retrofit.create(BrandApi.class);
        }
        return brandApi;
    }

    public static WebViewApi getWebViewApi() {
        if (webViewApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Constant.BAND_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            webViewApi = retrofit.create(WebViewApi.class);
        }
        return webViewApi;
    }

    public static WeiXinApi getWeiXinApi() {
        if (weiXinApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Constant.WEIXIN_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            weiXinApi = retrofit.create(WeiXinApi.class);
        }
        return weiXinApi;
    }

}
